﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using DBSourceToolsLib;
using DBConnectionLib;

namespace DBSourceTools
{
    public partial class OutputWindow : DockContent
    {
        //string m_OutputText = "";
        string m_QueryName;
        delegate void SetTextCallback(string text);
        StringBuilder m_StringBuilder ;

        public OutputWindow(string strQueryName)
        {
            InitializeComponent();
            this.m_QueryName = strQueryName;
        }
        public void setText(string newText)
        {
            this.m_textBoxOutput.Text = newText;
        }

        private void hideToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        public void RunScript(string strDatabase, string strObjectType, string strName, string strSelectedText)
        {
            this.m_StringBuilder = null;
            this.m_StringBuilder = new StringBuilder();
            this.m_textBoxOutput.Text = "";
            Database oDatabase = UserSettings.Instance().getSourceDatabase(strDatabase);
//            DBSourceToolsLib.ScriptRunner oScriptRunner = new ScriptRunner(oDatabase);
            //oScriptRunner.ProgressEventPublisher += new EventHandler<ProgressEventArgs>(oScriptRunner_ProgressEventPublisher);

            //string strExecuteText = "";
            //if (strSelectedText.Length > 0)
            //{
            //    strExecuteText = strSelectedText;
            //}
            //else
            //{
            //    DBSourceToolsLib.SysObjects.SysObject oSysObject =
            //        oDatabase.SysObjects.getSysObject(strObjectType, strName);
            //    strExecuteText = oSysObject.Text;
            //}

            //oScriptRunner.RunScript(this.m_QueryName, strExecuteText);
            //string output = oDatabase.ExecuteQuery(strName, strExecuteText);
            //this.m_textBoxOutput.Text = output;
        }

        void oScriptRunner_ProgressEventPublisher(object sender, ProgressEventArgs e)
        {
            this.m_StringBuilder.AppendLine(e.Message);
            switch (e.Percent)
            {
                case "100":
                    this.SetText(this.m_StringBuilder.ToString());
                    break;
            }
            //this.SetText(e.Message);
        }

        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.m_textBoxOutput.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.m_textBoxOutput.Text += text;
                this.m_textBoxOutput.Update();
            }
        }

    }
}
